package com.jifeng.br.pjj.bookreader.util.exception;

import com.jifeng.br.pjj.bookreader.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author PangJunjie
 * @Date 2023/7/11/011
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
//    private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(RTException.class)
    public R handlerRTException(RTException e) {
        log.info(e.getMsg());
        return R.error(e.getCode(), e.getMsg());
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public R handlerDuplicateKeyException(DuplicateKeyException e) {
        log.info(e.getMessage());
        return R.error("主键重复！");
    }

    @ExceptionHandler(Exception.class)
    public R handlerException(Exception e) {
        log.info(e.getMessage());
        return R.error(e.getMessage());
    }
}
